$(function() {
	init();
	initgame();
});


function initgame(){
	$.getJSON('/fw/game/game_getlist.action',function(data){
		var gameLevel;
		var data = data.rows;
		var str = "<option value=''>请选择游戏</option>";
		for(var i = 0; i < data.length;i++){
			str += "<option value="+data[i].gameId+">"+data[i].gameName+"</option>";
		}
		$('#gameId').html(str);
		$('#gameId').change(function(){
			var gameId = $('#gameId').val();
			if(gameId)
				initgamelevel(gameId);
		});
	});
}

function initgamelevel(gameId){
	$.getJSON('/fw/game/game_getbyid.action',{"gameId":gameId},function(data){
		var gameLevel;
		gameLevel = data.gameLevel;
		if(gameLevel > 0){
			var str = "<option value=''>请选择奖品等级</option>";
			for(var i = 1; i <= gameLevel;i++){
				str += "<option value=" + i + ">" + i + " 等奖</option>";
			}
			$('#level').html(str);
		}
	});
}

function successCallBack() {
	window.location.href = 'gamePrizeList.jsp';
}

/**
 * 提示信息处理
 */
function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle( {
		fieldMap : {},
		successMsg : '添加成功！',
		failMsg : '添加失败！错误：',
		success : successCallBack
	});
}





function init() {
	savehandle();
	changeActiveItem('gamePrizeList');
}

function add1() {
	$("#gamePrizeAddForm").ajaxSubmit({
		url: '/fw/game/prize_add.action',
		data: {},
		beforeSubmit: function() {
			return validateTooltip([{
				inputSel: '#gameId',
				validStr: 'empty'
			}, {
				inputSel: '#level',
				validStr: 'empty'
			},{
				inputSel: '#type',
				validStr: 'empty'
			}, {
				inputSel: '#prizeName',
				validStr: 'empty'
			}, {
				inputSel: '#probability',
				validStr: 'empty'
			}, {
				inputSel: '#prizeRemark',
				validStr: 'empty'
			}]);
		},
		success: function(r) {
			var data = $.parseJSON(r);
			if (data.success) {
				$MyAlert.alert('成功!', successCallBack);
			} else {
				$MyAlert.error(msg.errors);
			}
		}
	});
}

function add() {
	var gameId = $('#gameId').val();
	if (!gameId) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择游戏!',timer : 2000});
		return;
	}
	var level = $('#level').val();
	if (!level) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择奖品等级!',timer : 2000});
		return;
	}
	var probability = $('#probability').val();
	if (!probability) {
		$('#probability').savehandle('setmsg', {msg : '请填写中奖概率!',timer : 2000});
		return;
	}
	var type = $('#type').val();
	if (!type) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择奖品类型!',timer : 2000});
		return;
	}
	var prizeName = $('#prizeName').val();
	if (!prizeName) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写奖品名称!',timer : 2000});
		return;
	}
	var prizeRemark = $('#prizeRemark').val();
	if (!prizeRemark) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写奖品内容!',timer : 2000});
		return;
	}
	var prizeNum = $('#prizeNum').val();
	var data = {
		"level" : level,
		"prizeName": prizeName,
		"prizeRemark" : prizeRemark,
		"probability" : probability,
		"type" : type,
		"prizeNum" : prizeNum,
		"game.gameId" : gameId
	};
	$.getJSON('/fw/game/prize_add.action', data, function(data) {
		if (data.success) {
			$('#alertdiv').savehandle('setmsg', {
				msg : '成功!',
				timer : 2000
			});
			successCallBack();
		} else {
			var msg = data.result;
			$('#alertdiv').savehandle('setmsg', {
				msg : msg.errors,
				timer : 2000
			});
		}
	});
}
